let bookslist = [
  {
    id: 1,
    name: "西游记",
    author: "吴承恩",
    des: "师徒四人上西天求佛经的故事",
  },
];
let book = {};

let queryBooks = (req, res) => {
  res.send({
    status: 200,
    msg: "获取图书成功",
    bookslist,
  });
};

let addBook = (req, res) => {
  let id = bookslist[bookslist.length - 1].id + 1;
  let data = {
    ...req.body,
    id,
  };
  bookslist.push(data);
  res.send({
    status: 200,
    msg: "添加图书成功",
    bookslist,
  });
};

let delBook = (req, res) => {
  let id = req.params.id;
  bookslist = bookslist.filter((val) => val.id != id);
  res.send({
    status: 200,
    msg: "删除图书成功",
    bookslist,
  });
};

let queryBook = (req, res) => {
  let id = req.params.id;
[book] = bookslist.filter((val)=>val.id = id);
  res.send({
    status: 200,
    msg: "查询图书成功",
    book,
  });
};

let updateBook = (req, res) => {
  let id = book.id;
  let data = {
    id,
    ...req.body
  };
  bookslist = bookslist.map((val) => (val.id == id ? (val = data) : val));
  res.send({
      status:200,
      msg:'修改图书成功',
      bookslist
  })
};

let booksControl = {
  queryBooks,
  addBook,
  delBook,
  queryBook,
  updateBook,
};

module.exports = booksControl;
